//用户信息的store

import { defineStore } from "pinia"
import { reactive } from "vue"
import { getUser } from "@/api"


export const useUser = defineStore("user", ()=>{
    //定义状态数据(state)
    const user = reactive({
        id:"",
        username:"",
        password:"",
        salt:"",
        avatar:"",
        state:""
    })

    //修改用户信息的方法(action)
    const updateUser = async ()=>{
        const res = await getUser()
        if(res.data.errno == 0){
            user.id = res.data.data.id
            user.username = res.data.data.username
            user.password = res.data.data.password
            user.salt = res.data.data.salt
            user.avatar = res.data.data.avatar
            user.state = res.data.data.state
        }
    }

    //移除用户信息的方法(action)
    const removeUser = ()=>{
        user.id = ""
        user.username = ""
        user.password = ""
        user.salt = ""
        user.avatar = ""
        user.state = ""
    }

    //以对象形式返回
    return { user, updateUser, removeUser }
},{
    persist:{
        key:"user",
        storage:sessionStorage
    }
})
